package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ShoppingCartImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //查询当前购物车
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断是否在购物车中
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        //如果存在数量加一;
        if (shoppingCartList != null && shoppingCartList.size() == 1) {

            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            //更新数据
            shoppingCartMapper.updateNumber(shoppingCart);

        } else {
            //如果不存在，则添加 判断添加物品为菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                //返回类型为BigDecimal 由java.math包中API提供的 对超过16位有效位的数进行精准计算 避免丢失精准
                shoppingCart.setAmount(dish.getPrice());
            } else {
                //不是菜品就只剩下套餐了
                //前端数据获取套餐ID 使用SQL语句查询套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                //返回类型为BigDecimal 由java.math包中API提供的 对超过16位有效位的数进行精准计算 避免丢失精准
                shoppingCart.setAmount(setmeal.getPrice());
            }
            //第一次添加物品数量设置为1件
            shoppingCart.setNumber(1);
            //设置创建时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            //调用方法添加
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> showShoppingCart() {
        ShoppingCart shoppingCart = ShoppingCart.
                builder().
                userId(BaseContext.getCurrentId()).
                build();
        return shoppingCartMapper.list(shoppingCart);
    }

    @Override
    public void update(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //获取当前购物车
        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);

        shoppingCart= shoppingCartList.get(0);

        if (shoppingCart.getNumber() >1) {
            shoppingCart.setNumber(shoppingCart.getNumber()-1);
            //更新数据库
            shoppingCartMapper.updateNumber(shoppingCart);
        }else if (shoppingCart.getNumber()==1){
            shoppingCartMapper.deleteById(shoppingCart);
        }
    }

    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //获取当前购物车
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.delete(shoppingCart);

    }
}
